//Documentation complete
/**
 * @author Alex
 * @version 2009AP05
 */

package control;

import problemDomain.*;
import static problemDomain.ErrorBean.ErrorCode.*;
import broker.*;

import java.util.List;

/**
 * Control object for patron account object operations.
 */
public class PatronAccountControl extends Control {
	
	/* Attributes */
	
	private RegisteredPatronAccountBroker rpb;
	
	/* Attributes */
	
	
	/* Constructors */
	
	/**
	 * Constructs a <code>PatronAccountControl</code>.
	 * @throws ErrorBean if the database is not available
	 */
	public PatronAccountControl () throws ErrorBean {
		this.rpb = RegisteredPatronAccountBroker.getBroker();
	}
	
	/* END Constructors */
	
	
	/* Methods */
	
	/**
	 * Creates a patron account.
	 * @param firstname the first name of the patron
	 * @param lastname the last name of the patron
	 * @param phone the phone number of the patron
	 * @param address the address of the patron
	 * @param city the city of the patron's residence
	 * @param province the province of the patron's residence
	 * @param postalcode the postal code of the patron's residence
	 * @param email the email address of the patron
	 * @param username the username of the patron account
	 * @param password the password of the patron account
	 * @throws ErrorBean if the input data are not correct
	 */
	public void create (String firstname, String lastname, String phone, 
			String username, String password, String email, String address, 
			String city, String province, String postalcode) throws ErrorBean {
		RegisteredPatronAccountBean regPat = new RegisteredPatronAccountBean(
			0, firstname, lastname, phone, username, password,
			email, address, city, province, postalcode, null);
		rpb.persist(regPat);
	}
	
	/**
	 * Searches for patron accounts which match the search criteria.
	 * @param firstname the first name to search for (<code>""</code> excludes this field when searching)
	 * @param lastname the last name to search for (<code>""</code> excludes this field when searching)
	 * @param username the user name to search for (<code>""</code> excludes this field when searching)
	 * @param phone the phone number to search for (<code>""</code> excludes this field when searching)
	 * @param email the email address to search for (<code>""</code> excludes this field when searching)
	 * @param city the city to search for (<code>""</code> excludes this field when searching)
	 * @param province the province to search for (<code>""</code> excludes this field when searching)
	 * @param postalcode the postal code to search for (<code>""</code> excludes this field when searching)
	 * @return a <code>List</code> of results
	 * @throws ErrorBean if the input data are not correct
	 */
	public List<RegisteredPatronAccountBean> search (String firstname, 
			String lastname, String username, String phone, String email,
			String city, String province, String postalcode) throws ErrorBean {
		RegisteredPatronAccountBean bean = new RegisteredPatronAccountBean();
		if (firstname != null && !"".equals(firstname))
			bean.setFirstname(firstname);
		
		if (lastname != null && !"".equals(lastname))
			bean.setLastname(lastname);
		
		if (username != null && !"".equals(username))
			bean.setUsername(username);
		
		if (phone != null && !"".equals(phone))
			bean.setPhone(phone);
		
		if (email != null && !"".equals(email))
			bean.setEmail(email);
		
		if (city != null && !"".equals(city))
			bean.setCity(city);
		
		if (province != null && !"".equals(province))
			bean.setProvince(province);
		
		if (postalcode != null && !"".equals(postalcode))
			bean.setPostalCode(postalcode);
		
		return rpb.search(bean);
	}
	
	/**
	 * Updates a patron account.
	 * @param id the ID of the patron account
	 * @param firstname the first name of the patron
	 * @param lastname the last name of the patron
	 * @param phone the phone number of the patron
	 * @param address the address of the patron
	 * @param city the city of the patron's residence
	 * @param province the province of the patron's residence
	 * @param postalcode the postal code of the patron's residence
	 * @param email the email address of the patron
	 * @param username the username of the patron account
	 * @param password the password of the patron account
	 * @return the patron account
	 * @throws ErrorBean if the input data are not correct
	 */
	public RegisteredPatronAccountBean update (String id,String firstname,
			String lastname, String phone, String username, String password,
			String email, String address, String city, String province,
			String postalcode) throws ErrorBean {
		//FIXME A patron cannot ever change their password with this model
		//Check for correct password
		rpb.validateLogon(new RegisteredPatronAccountBean(username, password));
		
		RegisteredPatronAccountBean regPat = new RegisteredPatronAccountBean(
			checkPatronId(id), firstname, lastname, phone, username, password,
			email, address, city, province, postalcode, null);
		rpb.persist(regPat);
		return regPat;
	}
	
	/**
	 * Deletes a patron account.
	 * @param id the ID of the patron account
	 * @throws ErrorBean if the input data are not correct
	 */
	public void delete (String id) throws ErrorBean {
		rpb.remove(new RegisteredPatronAccountBean(checkPatronId(id)));
	}
	
	/**
	 * Deletes a patron account.
	 * @param username the username of the patron account
	 * @param password the password of the patron account
	 * @throws ErrorBean if the input data are not correct
	 */
	public void delete (String username, String password) throws ErrorBean {
		//FIXME this method does nothing
		rpb.validateLogon(new RegisteredPatronAccountBean(username, password));
	}
	
	/**
	 * Lists all patron accounts.
	 * @return a <code>List</code> of results
	 * @throws ErrorBean if there is an error retrieving the data
	 */
	public List<RegisteredPatronAccountBean> listAll () throws ErrorBean {
		List<RegisteredPatronAccountBean> list = rpb.listAll();
		return list;
	}
	
	/**
	 * Validates ta username and password.
	 * @param username the username of the patron account
	 * @param password the password of the patron account
	 * @return the authenticated patron
	 * @throws ErrorBean if the input data is not correct
	 */
	public RegisteredPatronAccountBean validate (String username, String password) throws ErrorBean {
		return rpb.validateLogon(
			new RegisteredPatronAccountBean(
				checkUsername(username),
				checkPassword(password)));
	}
	
	/* END Methods */
	
	
	/* Data Checks */
	
	private int checkPatronId (String id) throws ErrorBean {
		return checkInt(id, PATRON_ID_INVALID, this);
	}
	
	private String checkUsername (String username) throws ErrorBean {
		return checkString(username, PATRON_INCORRECT_USERNAME_OR_PASSWORD, this, false);}
	
	private String checkPassword (String password) throws ErrorBean {
		return checkString(password, PATRON_INCORRECT_USERNAME_OR_PASSWORD, this, false);}
	
	/* END Data Checks */
	
}
